package com.upcode.weighing.feignservice;

import com.upcode.weighing.common.enums.HttpStatus;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author ZYW
 * @date 2018/6/9
 */
public class BaseResult<T> implements Serializable {

    private Integer code;
    private String msg;
    private String timestamp;
    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> BaseResult<T> success(String message){
        return build(HttpStatus.OK, null,message);
    }

    public static <T> BaseResult<T> success(T data){
        return build(HttpStatus.OK, data,null);
    }

    public static <T> BaseResult<T> error(String message){
        return build(HttpStatus.BAD_REQUEST,null,message);
    }

    public static <T> BaseResult<T> error(T data){
        return build(HttpStatus.BAD_REQUEST, data,null);
    }

    public static <T> BaseResult<T> error(HttpStatus code, T data){
        return build(code,data,null);
    }

    private static <T> BaseResult<T> build(HttpStatus code, T data,String message){
        BaseResult<T> baseResult =  new BaseResult<T>();
        baseResult.setCode(code.value());
        baseResult.setData(data);
        baseResult.setMsg(message);
        baseResult.setTimestamp(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
        return baseResult;
    }

}
